/*
 * Filename:    UploadFileServiceImpl.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-1-21
 */
package com.paic.mhis.hcpms.common.uploadFile.biz.service.impl;

import java.util.Random;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;

import com.paic.mhis.common.queue.biz.service.MessagePublish;
import com.paic.mhis.common.redis.biz.service.RedisClientCallback;
import com.paic.mhis.common.redis.biz.service.RedisService;
import com.paic.mhis.hcpms.common.uploadFile.biz.service.UploadFileService;

@Component("uploadFileService")
public class UploadFileServiceImpl implements UploadFileService {

	private static final Logger logger = Logger.getLogger(UploadFileServiceImpl.class);
	@Value("${filePublishTitle}")
	private String filePublishTitle;
	@Resource(name = "redisQueuePublish")
	private MessagePublish redisQueuePublish;
	@Resource
	private RedisService redisService;

	// 发布消息
	@Override
	public void publishMsg(final byte[] fileContent, final String filename, final String path) {
		Random rand = new Random();
		int randomInt = rand.nextInt(9999);
		final String key = "uploadFile" + randomInt + System.currentTimeMillis();
		redisService.access(new RedisClientCallback<Boolean>() {
			@Override
			public Boolean exec(Jedis jedis) {

				logger.info("data length is " + fileContent.length);
				// 使用缓存存储内容
				jedis.hset(key, "filename", filename);
				jedis.hset(key, "path", path);
				jedis.hset(key.getBytes(), "filedata".getBytes(), fileContent);
				// 缓存有效时间
				jedis.expire(key, 60 * 3);
				return Boolean.TRUE;
			}
		});
		logger.info("filePublishTitle -------------" + filePublishTitle);
		logger.info("key-------------- " + key);
		redisQueuePublish.publishMessage(filePublishTitle, key);

	}
}
